<?php

use com\gdl\newagk\interfaces\ArchivesServiceIf;
use com\gdl\common\dataobject\common\ResultDO;
use com\gdl\newagk\dataobject\archives\ArchivesRequestDTO;


class api_ArchivesService extends api_Abstract implements ArchivesServiceIf
{
	
	//档案表
	public function archivesList(ArchivesRequestDTO $archives)
    {
		$result = new ResultDO();
		$filename = $archives->filename ? $archives->filename : [];
		$down = empty($filename['down']) ? '' : $filename['down'];
		$downid = $filename['id'] ?  explode(',',$filename['id']) : '';

		
		$page = ($filename['page']-1) * $filename['pagesize'];
		
		if($down=='excels'){
			 $groupBy = ' order by id desc ';
		} else {
			$groupBy = ' order by id desc limit '.$page.','.$filename['pagesize'];
		}
	//	$condition = " reg='{$filename['reg']}'";
		$condition = ' 1 ';
        if(!empty($filename['title'])){
            $condition .= " and (usertruename like '%{$filename['title']}%' or identity_id='".$filename['title']."' or idcard like '%{$filename['title']}%' )";
        }
		
		if($filename['platform_id']){
			$condition .= ' and platformId='.$filename['platform_id'];
		}
		if($filename['companyid']){
			$condition .= ' and companyId='.$filename['companyid'];
		}
		
		if(!empty($filename['exam'])){
            $condition .= " and (basicName like '%{$filename['exam']}%' or paperName like '%{$filename['exam']}%' )";
        }
		
		if($filename['examStatus']){
			$condition .= ' and status='.$filename['examStatus'];
		}
		
		if($filename['usual']){
			$condition .= ' and usual='.$filename['usual'];
		}
		
		if($filename['pass']=='pass'){
			$condition .= ' and pass=1';
		} else if($filename['pass']=='nopass'){
			$condition .= ' and pass=0';
		}
		if(!empty($filename['startScore']) && !empty($filename['endScore'])){
			if($filename['endScore']>$filename['startScore']){
				$endScore = $filename['endScore'];
				$startScore = $filename['startScore'];
				$filename['endScore'] = $endScore;
				$filename['startScore'] = $startScore;
			}
			
		}
		if(!empty($filename['startScore'])){
            $condition .= ' and score>='.$filename['startScore'];
        }
		
		if(!empty($filename['endScore'])){
            $condition .= ' and score<='.$filename['endScore'];
        }
		
		if(!empty($filename['startime'])){
            $condition .= ' and UNIX_TIMESTAMP(startTime)>='.$filename['startime'];
        }
		
		if(!empty($filename['endtime'])){
            $condition .= ' and UNIX_TIMESTAMP(startTime)<='.$filename['endtime'];
        }
		
		if($filename['terminal']){
			$condition .= ' and terminal='.$filename['terminal'];
		}

		if($down=='excels' && !empty($downid) ){
				$dow_id = [0];
				foreach($downid as $v){
					$dow_id[] = intval($v);
				}
				$condition = ' id in('.implode(',',$dow_id).')';
		}
				
	try {
				$archivesmark = new model_newagk_archivesmark();
				
				if($down=='excels'){
					
					$archmark = $archivesmark->select($condition,'usertruename,sex,ages,platformName,companyName,basicName,paperName,startTime,usual,score,remark,terminal',$groupBy)->items;
					$res = [];
					$terminal = ['1'=>'手机app','2'=>'平板app','3'=>'pc端'];
					$usual = ['1'=>'正常','2'=>'考试异常','3'=>'身份异常','4'=>'缺考'];
					foreach($archmark as $v){
					
						
						$res[] = [
								'usertruename'=>$v['usertruename'],//'姓名'
								'sex'=>$v['sex'] ? '女' : '男',//'性别'
								'ages'=>$v['ages'],//'年龄'
								'platformName'=>$v['platformName'],//'所属平台'
								'companyName'=>$v['companyName'],//'所属公司'
								'basicName'=>$v['basicName'],//'所属考场'
								'paperName'=>$v['paperName'],//'试卷名称'
								'startTime'=>$v['startTime'],//'开考时间'
								'score'=>$v['score'],//'考试分数'
								'usual'=>empty($usual[$v['usual']]) ? '异常' : $usual[$v['usual']],//'是否异常'
								'remark'=>$v['remark'],//'考试说明'
								'terminal'=>empty($terminal[$v['terminal']]) ? '未知终端' : $terminal[$v['terminal']],//'考试终端'
								];
					}
				
					
					
				} else {
					$res = $archivesmark->select($condition,'id,usertruename,identity_id,idcard,platformName,companyName,basicName,paperName,startTime,status,score,pass,usual,remark,terminal,sex,ages',$groupBy)->items;
					$num = $archivesmark->selectOne($condition,'count(*) num');

					$result->message =$num['num']; 
				}
				$result->data = $res;
				if(!empty($res[0])){
					$result->code = 1;
				} else {
					$result->code = 0;
				}
                
                $result->success = true;
                return $result;
            } catch (Exception $e) {
                $result->success = false;
                $result->code = $e->getCode();
                $result->message = $e->getMessage();
            }
            return $result;
	}
	//查看试卷
	public function archivesPaper(ArchivesRequestDTO $archives)
    {
		$result = new ResultDO();
		$id = $archives->filename['id'] ? gdl_lib_BaseUtils::getStr($archives->filename['id'],'int') : 0;

	try {
				$archivesmark = new model_newagk_archivesmark();
				$res = $archivesmark->select('id='.$id,'paperName,bankName,questions,answer,duration,passscore,questionsNum,usertruename,idcard,startTime,time,pass,remark,score,usual')->items;
				$global = new service_globals();
				
				$result->data = $res;
				$result->message = json_encode($global->types);
				if($res){
					$result->code = 1;
				} else {
					$result->code = 0;
				}
                $result->success = true;
                return $result;
            } catch (Exception $e) {
                $result->success = false;
                $result->code = $e->getCode();
                $result->message = $e->getMessage();
            }
            return $result;
	}
	
	//归档
	public function archivesFile(ArchivesRequestDTO $archives)
    {
		$result = new ResultDO();
		$id = $archives->filename['id'] ? gdl_lib_BaseUtils::getStr($archives->filename['id'] ,'int') : 0;
		$types = $archives->filename['types']=='ones' ? 1 : 0;

	try {
				$archivesmark = new model_newagk_archivesmark();
				$examspapers = new model_newagk_examspapers();
				$res = true;
				if($types){
					$papers = $examspapers->selectOne(['basicid'=>$id],'id,identity_id');
					if(!empty($papers['id'])){
						$archivesmark->delete('identity_id='.$papers['identity_id'].' and basicId='.$id);
					}
					$res = $archivesmark->archives($id);
				} else {
					$papers = $examspapers->select(['basicid'=>$id],'id,identity_id')->items;
					$identity_id=[0];
					foreach($papers as $v){
						$identity_id[$v['identity_id']] = $v['identity_id'];
					}
					/****处理已经考试的****/
					$archivesmark->delete('identity_id in('.implode(',',$identity_id).') and basicId='.$id);

					foreach($papers as $v){
						$res = $archivesmark->archives($v['id']);
						usleep(500);
					}
					

					/****处理已经考试的 end****/
					
					/****处理缺考的****/
					$basicusers = new model_newagk_basicuser();
					$missExam = $basicusers->getInfoArg('identity_id not in('.implode(',',$identity_id).') and basic_id='.$id,'id,identity_id');

					if(!empty($missExam)){
						$miss_id=[0];
						foreach($missExam as $v){
							$miss_id[$v['identity_id']] = $v['identity_id'];
						}
						
						$archivesmark->delete('identity_id in('.implode(',',$miss_id).') and basicId='.$id);
						foreach($missExam as $v){
							$resw = $archivesmark->missArchives($v['id']);
							usleep(500);
						}
					
					}

					/****处理缺考的 end****/
				}
	
				if($res){
					$result->code = 1;
				} else {
					$result->code = 0;
				}
                $result->success = true;
                return $result;
            } catch (Exception $e) {
                $result->success = false;
                $result->code = $e->getCode();
                $result->message = $e->getMessage();
            }
            return $result;
	}

}

